package com.rocky.project.util;
import org.apache.log4j.Logger;



public class PageUtil {  
	protected static Logger logger = Logger.getLogger(PageUtil.class);
//    protected static Log logger =LogFactory.getFactory().getInstance(PageUtil.class);
    public static Page createPage(Page page, int totalRecords){ 
        return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords); 
    } 
    public static Page createPage(int everyPage, int currentPage, int totalRecords){ 
    	int everyPage2 = getEveryPage(everyPage); 
    	int currentPage2 = getCurrentPage(currentPage); 
        int beginIndex = getBeginIndex(everyPage2, currentPage2); 
        int totalPage = getTotalPage(everyPage2, totalRecords); 
        boolean hasNextPage = hasNextPage(currentPage2, totalPage); 
        boolean hasPrePage = hasPrePage(currentPage2);         
        return new Page(hasPrePage, hasNextPage,everyPage2, totalPage, currentPage2, beginIndex,totalRecords); 
    } 
    
    private static int getEveryPage(int everyPage){ 
        return everyPage == 0 ? 10 : everyPage; 
    } 
    
    private static int getCurrentPage(int currentPage){ 
        return currentPage == 0 ? 1 : currentPage; 
    } 
    
    private static int getBeginIndex(int everyPage, int currentPage){ 
        return (currentPage - 1) * everyPage; 
    } 
        
    public static int getTotalPage(int everyPage, int totalRecords){ 
        int totalPage = 0;                 
        if(totalRecords % everyPage == 0){ 
            totalPage = totalRecords / everyPage; 
        }else{ 
            totalPage = totalRecords / everyPage + 1 ; 
        }        
        return totalPage; 
    }     
    private static boolean hasPrePage(int currentPage){ 
        return currentPage == 1 ? false : true; 
    }     
    private static boolean hasNextPage(int currentPage, int totalPage){ 
        return currentPage == totalPage || totalPage == 0 ? false : true; 
    } 
    

} 

